package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.service.IWmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.wemedia.service.IWmNewsService;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 自媒体图文内容信息表 前端控制器
 * </p>
 *
 * @author heima
 * @since 2021-03-16
 */
@RestController
@RequestMapping("/api/v1/news")
@Api(tags = "自媒体图文内容信息表接口")
@CrossOrigin
public class WmNewsController{

    @Autowired
    private IWmNewsService wmNewsService;

    /**
     * 分页条件查询自媒体文章
     */
    @PostMapping("/page")
    public PageResult<WmNewsDto> findByPage(@RequestBody @Valid WmNewsPageReqDto dto){
       return wmNewsService.findByPage(dto);
    }

    /**
     * 保存、修改、提交草稿功能
     */
    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody @Valid WmNewsDto dto){
        wmNewsService.saveNews(dto);
        return ResponseResult.ok();
    }

    @Autowired
    private IWmUserService wmUserService;

    /**
     * 基于文章id查询文章功能
     */
    @GetMapping("/one/{id}")
    public ResponseResult<WmNewsResultDTO> findById(@PathVariable("id") Integer id){
        WmNews wmNews = wmNewsService.getById(id);
        if(wmNews==null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"自媒体文章不存在");
        }
        WmNewsResultDTO wmNewsResultDTO = BeanHelper.copyProperties(wmNews, WmNewsResultDTO.class);
        WmUser wmUser = wmUserService.getById(wmNews.getWmUserId());
        wmNewsResultDTO.setAuthorName(wmUser.getName());
        return ResponseResult.ok(wmNewsResultDTO);
    }

    /**
     * 删除自媒体文章功能
     */
    @DeleteMapping("/del_news/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        wmNewsService.deleteById(id);
        return ResponseResult.ok();
    }

    /**
     * 自媒体文章上下架功能
     */
    @PutMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto wmNewsDto){
        wmNewsService.downOrUp(wmNewsDto);
        return ResponseResult.ok();
    }

    /**
     * 更新自媒体新闻状态
     * @param wmNewsDto
     * @return
     */
    @PutMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNewsDto wmNewsDto){
        wmNewsService.updateWmNews(wmNewsDto);
        return ResponseResult.ok();
    }

    /**
     * 查询审核通过待发布，并且发布时间小于当前时间文章id列表
     */
    @GetMapping("/findPub")
    public List<Integer> findWmNewsIds(){
        return wmNewsService.findWmNewsIds();
    }

    /**
     * 自媒体文章审核列表查询
     */
    @PostMapping("/list")
    public PageResult<WmNewsResultDTO> findList(@RequestBody NewsAuthDto dto){
       return wmNewsService.findList(dto);
    }

}
